package com.sztle.admodule.control.utils;

import static android.content.Intent.ACTION_BATTERY_CHANGED;

import android.Manifest;
import android.content.ContentResolver;
import android.content.Context;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.hardware.Sensor;
import android.hardware.SensorManager;
import android.media.AudioManager;
import android.provider.Settings;
import android.telephony.TelephonyManager;
import android.text.TextUtils;

import androidx.core.app.ActivityCompat;

import com.sztle.admodule.GmAdManager;
import com.sztle.admodule.outstatus.AppStatusProcessor;
import com.sztle.admodule.someutils.DeviceUtils;
import com.sztle.admodule.someutils.NetworkUtils;
import com.sztle.admodule.someutils.PhoneUtils;
import com.sztle.admodule.someutils.SPUtils;
import com.sztle.admodule.someutils.ScreenUtils;
import com.sztle.admodule.someutils.Utils;
import com.sztle.admodule.control.manager.BatteryStatusReceiver;

import java.util.List;

public class UserDeviceUtils {


    private static final String KEY_SAVE_DID = "did";

    //是否开启了开发者选项
    public static boolean isDevelopSet() {
        return DeviceUtils.isAdbEnabled() || DeviceUtils.isDevelopmentSettingsEnabled();
    }

    //是否开启VPN
    public static boolean isVpn() {
        return NetworkUtils.isUsingVPN();
    }

    //是否模拟器
    public static boolean isEmulator() {
        return DeviceUtils.isEmulator();
    }

    //是否root设备
    public static boolean isRootDevice() {
        return DeviceUtils.isDeviceRooted();
    }

    //是否为没有sim卡
    public static boolean isNoSim() {
        TelephonyManager telephonyManager = (TelephonyManager) Utils.getApp().getSystemService(Context.TELEPHONY_SERVICE);
        if (telephonyManager.getSimState() == TelephonyManager.SIM_STATE_UNKNOWN
                || telephonyManager.getSimState() == TelephonyManager.SIM_STATE_ABSENT) {
            return true;
        }
        return false;
    }


    public static boolean isUsbPlugin() {
        BatteryStatusReceiver batteryChangeReceiver = sBatteryChangeReceiver;
        boolean isUsb = (batteryChangeReceiver != null && batteryChangeReceiver.isUsbPlugin());
        return isUsb;
    }

    //是否充电设备
    public static boolean isCharging() {
        BatteryStatusReceiver batteryChangeReceiver = sBatteryChangeReceiver;
        boolean isCharge = (batteryChangeReceiver != null && batteryChangeReceiver.isCharging());
//        if(!isCharge){
//            BatteryManager batteryManager = (BatteryManager) Utils.getApp().getSystemService(Context.BATTERY_SERVICE);
//            isCharge = batteryManager.isCharging();
//        }
        return isCharge;

    }

    public static boolean isElectricityStatus() {
        BatteryStatusReceiver batteryChangeReceiver = sBatteryChangeReceiver;
        return (batteryChangeReceiver != null && batteryChangeReceiver.isElectricityStatus());
    }

    private static BatteryStatusReceiver sBatteryChangeReceiver;

    public static void registerBatteryChangeListener(Context context) {
        if (sBatteryChangeReceiver == null) {
            sBatteryChangeReceiver = new BatteryStatusReceiver();
            IntentFilter intentFilter = new IntentFilter();
            intentFilter.addAction(ACTION_BATTERY_CHANGED);
            context.registerReceiver(sBatteryChangeReceiver, intentFilter);
        }
    }

    public static void unRegisterBatteryChangeListener(Context context) {
        BatteryStatusReceiver batteryChangeReceiver = sBatteryChangeReceiver;
        if (batteryChangeReceiver == null) {
            context.unregisterReceiver(batteryChangeReceiver);
            sBatteryChangeReceiver = null;
        }
    }

    /**
     * 1.获取系统默认屏幕亮度值 屏幕亮度值范围（0-255）
     **/
    private static int getScreenBrightness() {
        ContentResolver contentResolver = Utils.getApp().getContentResolver();
        int defVal = 125;
        return Settings.System.getInt(contentResolver,
                Settings.System.SCREEN_BRIGHTNESS, defVal);
    }

    public static boolean isBrightnessBad() {
        return getScreenBrightness() < 0 || getScreenBrightness() > 255;
    }

    //是否为常亮设备
    public static boolean isAlwaysScreenOn() {
        if (ScreenUtils.getSleepDuration() >= 10 * 60 * 60 * 1000) {
            return true;
        }
        return false;
    }

    public static boolean hasGravitySensor() {
        SensorManager sensorManager = (SensorManager) Utils.getApp().getSystemService(Context.SENSOR_SERVICE);
        if (sensorManager != null) {
            List<Sensor> sensorList = sensorManager.getSensorList(Sensor.TYPE_ALL);
            for (Sensor sensor : sensorList) {
                if (sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
                    return true; // 设备有加速度传感器，通常意味着有重力感应
                }
            }
        }
        return false; // 没有找到加速度传感器
    }


    /**
     * 是否开启了代理
     *
     * @return
     */
    public static boolean isWifiProxy() {
        String proxyAddress = System.getProperty("http.proxyHost");
        int proxyPort = 0;
        String portStr = System.getProperty("http.proxyPort");
        if (!TextUtils.isEmpty(portStr)) {
            proxyPort = Integer.parseInt(portStr);
        }
        boolean wifiProxy = !TextUtils.isEmpty(proxyAddress) && proxyPort != 0;
        return wifiProxy;
    }

    //音量状态
    public static boolean isVolumeStatus() {
        AudioManager audioManager = (AudioManager) Utils.getApp().getSystemService(Context.AUDIO_SERVICE);
        int volume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
        int max = audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
        if (volume < 0 || volume > max) {
            return true;
        }
        return false;
    }

    public static String getAndroidId() {
        return "";
    }

    public static String getImei() {
        if (ActivityCompat.checkSelfPermission(Utils.getApp(), Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
            return "";
        }
        return PhoneUtils.getIMEI();

    }

    public static String getUUId() {
//        String androidId = getAndroidId();
//        if (!TextUtils.isEmpty(androidId)) {
//            return HashDigits.getMd5Str(androidId);
//        }
        return DeviceUtils.getUniqueDeviceId("uuid");
    }

    public static String getDid() {
        String did = GmAdManager.getDid();
        if (TextUtils.isEmpty(did)) {
            did = SPUtils.getInstance().getString(KEY_SAVE_DID);
        } else {
            SPUtils.getInstance().put(KEY_SAVE_DID, did);
        }
        return did;
    }

    public static boolean isXiaoMi() {
        return AppStatusProcessor.getChannel().toLowerCase().equals("xiaomi");
    }

    public static boolean isHuaWei() {
        return AppStatusProcessor.getChannel().toLowerCase().equals("huawei");
    }

    public static boolean isVivo() {
        return AppStatusProcessor.getChannel().toLowerCase().equals("vivo");
    }

    public static boolean isOppo() {
        return AppStatusProcessor.getChannel().toLowerCase().equals("oppo");
    }

    public static boolean isHonor() {
        return AppStatusProcessor.getChannel().toLowerCase().equals("honor");
    }

    public static boolean isBaidu() {
        return AppStatusProcessor.getChannel().toLowerCase().equals("baidu");
    }
    public static boolean isGMore(){
        return AppStatusProcessor.getChannel().toLowerCase().equals("gremore");
    }
}
